//

// (  )
//

package com.tools.acceleratecore.xlib.xlib.impl;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadLooper;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadLooperListener;

public class XThreadLooper implements IXThreadLooper {
    private Thread mThread = null;
    private Runnable mRunnable = null;
    private Looper mLooper = null;
    public Handler mHandler = null;
    private IXThreadLooperListener mIXThreadLooperListener = null;
    private boolean mWork = false;
    private Object mObjLock = null;
    private static final int VALUE_INT_MESSAGEID_THREAD_LOOPER = 4096;

    public XThreadLooper() {
        this._init();
    }

    private void _init() {
        this.mObjLock = new Object();
        this.mRunnable = new Runnable() {
            public void run() {
                Looper.prepare();
                XThreadLooper.this.mLooper = Looper.myLooper();
                XThreadLooper.this.mHandler = new Handler() {
                    public void handleMessage(Message msg) {
                        if (4096 == msg.what) {
                            XThreadLooper.this.mLooper.quit();
                            XThreadLooper.this.clear();
                        } else if (XThreadLooper.this.mIXThreadLooperListener != null) {
                            XThreadLooper.this.mIXThreadLooperListener.onThreadLooperMessage(msg);
                        }

                    }
                };
                Looper.loop();
            }
        };
    }

    public boolean start(IXThreadLooperListener iXThreadLooperListener) {
        Object var2 = this.mObjLock;
        synchronized(this.mObjLock) {
            if (!this.mWork && iXThreadLooperListener != null) {
                this.mWork = true;
                this.mIXThreadLooperListener = iXThreadLooperListener;
                this.mThread = new Thread(this.mRunnable);
                this.mThread.start();
                return true;
            } else {
                return false;
            }
        }
    }

    public void stop() {
        Message msg = new Message();
        msg.what = 4096;
        this.mHandler.sendMessage(msg);
    }

    public boolean sendMessage(Message msg) {
        return msg == null ? false : this.mHandler.sendMessage(msg);
    }

    public boolean isWork() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            return this.mWork;
        }
    }

    private void clear() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            this.mWork = false;
            this.mIXThreadLooperListener = null;
            this.mThread = null;
        }
    }
}
